<template>
  <div class="me_search">
    <el-dialog
      title="添加人员"
      :visible.sync="SearchPersonnelTypeData.dialogVisible"
      class="small"
      height="50%"
      :before-close="handleClose"
    >
      <div class="box">
        <div class="left">
          <div class="left-top">
            <div class="inputDeep">
              <!--             :select="handleSelect"
                :fetch-suggestions="querySearchAsync" -->
              <el-autocomplete
                v-model="input"
                :fetch-suggestions="querySearchAsync"
                placeholder="搜索名字"
                @select="handleSelect"
              />
            </div>
            <div class="button">
              <el-tag
                v-for="(tag, index) in dataList"
                :key="index"
                closable
                :disable-transitions="false"
                class="tag"
                @close="handleCloses(tag)"
              >
                {{ tag.userName }}
              </el-tag>
            </div>
          </div>
          <div class="left-but">
            <el-button type="primary" @click="confirm">确 定</el-button>
            <el-button
              @click="SearchPersonnelTypeData.dialogVisible = false"
            >取 消</el-button>
          </div>
        </div>
        <div class="right">
          <div class="content">
            <p class="p3">{{ SearchPersonnelTypeData.title }}</p>
            <el-table
              ref="tableData"
              height="35vh"
              :data="getdata"
              style="width: 100%"
              :show-header="false"
              @cell-click="click"
              @selection-change="handleSelectionChange"
            >
              <el-table-column
                :selectable="checkboxT"
                type="selection"
                width="55"
              />
              <el-table-column>
                <template slot-scope="scope">
                  <div class="templateClass">
                    <span>
                      {{ scope.row.userName }}
                    </span>
                    <i
                      v-if="scope.row.children != null"
                      class="iconfont el-icon-allxiayiji i4"
                    />
                  </div>
                  <!-- <i class="el-icon-arrow-down"></i> -->
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer" />
    </el-dialog>
  </div>
</template>

<script>
import { searchUserByCode } from '@/api/uc/user.js'
import { searchUserByKeyword } from '@/api/uc/user.js'
import { Verify } from '@/utils/common'
import { set, get } from '@/utils/db'
import '@/styles/search.scss'
import { getDrafterUserGroup } from '@/api/xd/documentFlow.js'

export default {
  components: {},
  props: ['SearchPersonnelTypeData'],
  data() {
    return {
      dialogVisible: false,
      classifyList: [
        { id: '0', name: '角色', icon: 'el-icon-alljiaose' },
        { id: '1', name: '团队', icon: 'el-icon-alltuanduiguanli' }
      ],
      groupUserTree: [],
      roleUserTree: [],
      getdata: [],
      itembox: false,
      isActive: true,
      dataList: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      permissionIds: [],
      input: '',
      breadcrumb: [],
      terrData: [],
      checkedGh: [],
      activeNames: ['1'],
      restaurants: [],
      timeout: null,
      searchData: {}
    }
  },
  watch: {
    SearchPersonnelTypeData(val) {
      this.getsearch()
      const data = JSON.parse(get('personnelGather'))
      data.data.roleUserTree.forEach((item) => {
        if (item.label == val.title) {
          this.searchData.id = item.id
          console.log(item, 'dddd')
        }
      })
    }
  },
  mounted() {},
  methods: {
    dialogVisibles() {
      this.dialogVisible = true
      this.breadcrumb = []
    },

    get() {
      console.log(this.data)
    },
    getMsgFormSon(data) {
      this.dataList = data
    },
    handleClose(done) {
      this.dataList = ''
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 搜索

    querySearchAsync(queryString, cb) {
      // if (Verify(queryString) == true) {
      //   return
      // }
      // clearTimeout(this.timeout)

      // this.timeout =
      setTimeout(() => {
        searchUserByKeyword({
          status:0,
          pageIndex: 0,
          pageSize: 1,
          userName: queryString,
          type: 1,
          id: this.searchData.id
        }).then((res) => {
          res.data.forEach((item) => {
            item.value = item.userName
          })
          cb(res.data)
        })
      }, 1000 * Math.random())
    },
    handleSelect(item) {
      this.input = ''
      this.dataList = []
      this.dataList.push({ userId: item.userId, userName: item.userName })
      //   this.$refs.tableData.toggleRowSelection(this.dataList[0])
    },
    // 获取数据
    async getsearch() {
      if (this.SearchPersonnelTypeData.type === 'px') {
        const { data } = await getDrafterUserGroup({
          documentFlowId: this.SearchPersonnelTypeData.temp.documentFlowId
        })
        this.SearchPersonnelTypeData.title = data.userName
        this.getdata = data.children
      } else {
        const getdataList = await searchUserByCode({
          roleCode: this.SearchPersonnelTypeData.type
        })
        this.getdata = getdataList.data
      }
    },
    // tag回调
    handleCloses(tag) {
      this.$refs.tableData.toggleRowSelection()
      this.dataList.splice(this.dataList.indexOf(tag), 1)
    },
    click(row) {
      if (row.children) {
        this.getdata = row.children
        // this.checkboxT(this.getdata)
        this.breadcrumb.push({ label: row.label, id: row.id })
      }
    },

    checkboxT(row) {
      if (row.person == false) {
        return false
      } else {
        return true
      }
    },
    handleSelectionChange(selection) {
      this.dataList = selection
      if (selection.length > 1) {
        this.$refs.tableData.clearSelection()
        this.$refs.tableData.toggleRowSelection(selection.pop())
      }
    },
    goTo(id, index) {
      const idl = id
      this.getItem(idl, this.terrData)
      var flag = true
      for (var i = this.breadcrumb.length - 1; i >= 0; i--) {
        if (this.breadcrumb[i].id === idl) {
          flag = false
        }
        if (flag) {
          this.breadcrumb.splice(i, 1)
        }
      }
      //  .push({ label: this.getdata.label, id: this.getdata.id })
    },
    // 递归树
    getItem(id, data) {
      for (const dat in data) {
        const e = data[dat]
        if (e.id === id) {
          this.getdata = e.children
          return
        }
        if (e.children) {
          this.getItem(id, e.children)
        }
      }
    },
    confirm() {
      this.SearchPersonnelTypeData.dialogVisible = false
      this.$emit('dataList', this.dataList)
    }
  }
}
</script>

